package com.system;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统工具类
 * SystemUtil.java
 * 2018年4月26日 下午4:51:07
 * @author yaojinshan
 */
public class SystemUtil {
	
	public static final String OSNAME_WINDOWS = "window";
	public static final String OSNAME_MAC = "mac";
	public static final String OSNAME_LINUX = "linux";
	public static String CURRENT_OSNAME = "";//当前系统
	
	public static Map<String,String> filePathMap = new HashMap<String, String>();//文件路径集合map
	
	static{
		//初始化类成员属性
		String cur_os = System.getProperty("os.name");
		if(cur_os == null || cur_os.isEmpty()){
			checkOsNameException();
		}
		if(cur_os.toLowerCase().indexOf(OSNAME_WINDOWS) >= 0){
			CURRENT_OSNAME = OSNAME_WINDOWS;
		}else if(cur_os.toLowerCase().indexOf(OSNAME_MAC) >= 0){
			CURRENT_OSNAME = OSNAME_MAC;
		}else if(cur_os.toLowerCase().indexOf(OSNAME_LINUX) >= 0){
			CURRENT_OSNAME = OSNAME_LINUX;
		}else{
			checkOsNameException();
		}
		filePathMap.put(OSNAME_WINDOWS, "D:\\upload\\");
		filePathMap.put(OSNAME_MAC, "file://");
		filePathMap.put(OSNAME_LINUX, "/usr/local/bak/");
	}

	public static void main(String[] args) {
		System.out.println("当前操作系统：" + CURRENT_OSNAME);
		System.out.println("文件存储路径：" + getFilePath(CURRENT_OSNAME));
	}
	/**
	 * 获取当前系统的存储路径
	 * @param osName：系统标识
	 * @return
	 */
	public static String getFilePath(String osName){
		if(!filePathMap.containsKey(osName)){
			checkOsNameException();
		}
		return filePathMap.get(osName);
	}
	/**
	 * 检查系统异常
	 */
	public static void checkOsNameException(){
		throw new RuntimeException("未知的操作系统!");
	}

}
